#define _CRT_SECURE_NO_WARNINGS
#include"slist.h"

SListNode* BuySListNode(SLTDateType x)
{
	SListNode* p = (SListNode*)malloc(sizeof(SListNode));
	if (p == NULL)
	{
		perror("malloc:fail");
		return NULL;
	}
	p->data = x;
	p->next = NULL;
	return p;
}

void SListPrint(SListNode* phead)
{
	SListNode* cur = phead;
	while (cur)
	{
		printf("%d->", cur->data);
		cur = cur->next;
	}
	printf("NULL\n");
}

void SListPushBack(SListNode** pphead, SLTDateType x)
{
	SListNode* newnode = BuySListNode(x);
	if (*pphead == NULL)
	{
		*pphead= newnode;
	}
	else
	{
		SListNode* tail = *pphead;
		while (tail->next != NULL)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

void SListPushFront(SListNode** pphead, SLTDateType x)
{
	SListNode* newnode = BuySListNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;
	}
	else
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	
}

void SListPopBack(SListNode** pphead)
{
	assert(*pphead);
	SListNode* cur = *pphead;
	/*while (cur->next->next != NULL)
	{
		cur = cur->next;
	}
	free(cur->next);
	cur->next = NULL;*/
	SListNode* p = *pphead;
	while (cur->next != NULL)
	{
		p = cur;
		cur = cur->next;
	}
	free(p->next);
	p->next = NULL;
}


void SListPopFront(SListNode** pphead)
{
	assert(*pphead);
	SListNode* cur = *pphead;
	*pphead = cur->next;
	free(cur);
	cur = NULL;
}

SListNode* SListFind(SListNode* phead, SLTDateType x)
{
	assert(phead);
	SListNode* cur = phead;
	while (cur)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return NULL;
}

void SListInsertAfter(SListNode* pos, SLTDateType x)
{
	assert(pos);
	SListNode* newnode = BuySListNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}